//
//  WmcCommonSettingsDBManager.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/5/9.
//

import Foundation

// MARK: - 扩展WmcCommonSettingsDBManager + WmcDBProtocol

extension WmcCommonSettingsDBManager: WmcDBProtocol {
    static var tableName: String = "common_settings"

    static var dataType: [WmcDBManager.DataType] {
        return [
            .primaryKey(),
            .integer(name: "save_none_watermark_image"),
            .integer(name: "upload_none_watermark_image"),
            .integer(name: "hidden_official_mark"),
            .integer(name: "take_photo_to_preview"),
        ]
    }
}

// MARK: - [WmcCommonSettingsDBManager]

struct WmcCommonSettingsDBManager {
    /// <#Description#>
    /// - Parameters:
    ///   - model: <#model description#>
    ///   - resultBlock: 结果
    static func insert(model: WmcPreferenceCameraSettingsModel,
                       resultBlock: YPBlock.OneParam<Bool>? = nil)
    {
        let saveNoneWatermarkImage = model.saveNoneWatermarkImage ? 1 : 0
        let uploadNoneWatermarkImage = model.uploadNoneWatermarkImage ? 1 : 0
        let hiddenOfficialMark = model.hiddenOfficialMark ? 1 : 0

        let dataList: [WmcDBManager.InsertData] = [
            .init(column: Self.dataType[1], value: saveNoneWatermarkImage),
            .init(column: Self.dataType[2], value: uploadNoneWatermarkImage),
            .init(column: Self.dataType[3], value: hiddenOfficialMark),
        ]

        WmcDBManager.shared.insertData(tableName: Self.tableName, insertData: dataList, resultBlock: resultBlock)
    }

    /// <#Description#>
    /// - Parameter resultBlock: 结果
    static func query(resultBlock: YPBlock.OneParam<WmcPreferenceCameraSettingsModel?>? = nil) {
        WmcDBManager.shared.searchCustomData(tableName: Self.tableName) { list in
            let result: WmcPreferenceCameraSettingsModel? = WmcPreferenceCameraSettingsModel.deserialize(from: list.first)
            resultBlock?(result)
        }
    }
}
